package com.example.mcdonaldapplication.activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.widget.Toast
import com.example.mcdonaldapplication.databinding.ActivityLoginBinding
import com.example.mcdonaldapplication.info.ServerIP
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONObject
import kotlin.concurrent.thread

class LoginActivity : AppCompatActivity() {
    private lateinit var binding:ActivityLoginBinding
    private val serverIp = ServerIP.getIp()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)
        supportActionBar?.hide()

        val btnLogin = binding.btnLogin
        btnLogin.setOnClickListener {
            val username = binding.editUsername.text.toString()
            val password = binding.editPassword.text.toString()
            if(username == "" || password == ""){
                Toast.makeText(this,"请输入账号密码",Toast.LENGTH_SHORT).show()
            }else{
                login(username, password)
            }
        }

        val tvSignUp = binding.tvSignUp
        tvSignUp.setOnClickListener{
            val intent = Intent(this, RegisterActivity::class.java)
            finish()
            startActivity(intent)
        }
    }

    private fun login(username:String, password:String) {
        thread {
            Log.d("username", username)
            Log.d("password", password)
            //post方式提交的数据
            val formBody: FormBody = FormBody.Builder()
                .add("username", username)
                .add("password", password)
                .build()
            val client = OkHttpClient()
            val request = Request.Builder()
                .url("http://${serverIp}/login")
                .post(formBody)
                .build()
            val response = client.newCall(request).execute()
            val responseData = response.body.string()
            parseJsonLogin(responseData)
        }
    }

    private fun parseJsonLogin(jsonData: String) {
        try {
            val jsonObject = JSONObject(jsonData)
            val success = jsonObject.optString("success")
            if(success == "true") {
                val data = jsonObject.getJSONObject("data")
                val uid = data.optString("uid").toInt()
                val username = data.optString("username").toString()
                val password = data.optString("password").toString()
                val gender = data.optString("gender").toString()
                val mobile = data.optString("mobile").toString()
                val birthday = data.optString("birthday").toString()
                val integral = data.optString("integral").toInt()
                val balance = data.optString("balance").toString()
                val address = data.optString("address").toString()
                val tid = data.optString("tid").toString()
                val editor = getSharedPreferences("user", Context.MODE_PRIVATE).edit()
                editor.putInt("uid", uid)
                editor.putString("username", username)
                editor.putString("password", password)
                editor.putString("gender", gender)
                editor.putString("mobile", mobile)
                editor.putString("birthday", birthday)
                editor.putInt("integral", integral)
                editor.putString("balance", balance)
                editor.putString("address", address)
                editor.putString("tid", tid)
                editor.apply()
                finish()
            }else{
                Looper.prepare()
                Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show()
                Looper.loop()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}